var app = getApp()
//常量数据配置
var ConstantData = require('../../../../../../../common/constant_data.js')
var uitl = require('../../../../../../../utils/util.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    var that = this;
    var lbmId = options.lbmId;
    var hospitalName = options.hospitalName;
    that.setData({
      hospitalName: hospitalName,
      lbmId: lbmId
    });
    
    wx.showLoading({
      title: '加载中...',
      mask: true
    });

    app.executeRequest(function (header) {
      wx.request({
        url: ConstantData.CONSTATNT_DATA.BASE_URL + '/lease_admin/maintain/check_chair',
        data: {
          lbmId: lbmId
        },
        header: header,
        success: function (res) {
          getApp().hideLoading();
          var chairInfoList = res.data.chairInfoList;
          var noBackChairList = res.data.noBackChairList;
          var currentOnPipleChairList = res.data.currentOnPipleChairList;

          

          var chairCount = 0;
          var chairList = new Array();
          for (var i = 0; i < chairInfoList.length; i++) {
            chairCount += chairInfoList[i].counts;

            var arr = chairInfoList[i].ids.split(",");
            chairList.push.apply(chairList, arr);
          }

          var noBackCount = noBackChairList.length;
          var backChairCount = currentOnPipleChairList.length;

          //统计到的轮椅 
          var recordChairList = new Array();


          var ownNoBackChairCount = 0;
          var otherNoBackChairCount = 0;
          var ownNoBackChairList = new Array();
          var otherNoBackChairList = new Array();
          for (var i = 0; i < noBackChairList.length; i++) {
            noBackChairList[i].uur_open_lock_time = uitl.timestampToTime(noBackChairList[i].uur_open_lock_time);

            noBackChairList[i].last_update = uitl.timestampToTime(noBackChairList[i].last_update);

            recordChairList.push(noBackChairList[i].uur_equipmnet_id);

            if (noBackChairList[i].hp_hpspital == hospitalName) {
              ownNoBackChairCount++;
              ownNoBackChairList.push(noBackChairList[i]);
            } else {
              otherNoBackChairCount++;
              otherNoBackChairList.push(noBackChairList[i]);
            }

          }

          var ownChairCount = 0;
          var otherChairCount = 0;
          var ownBackChairList = new Array();
          var otherBackChairList = new Array();
          for (var i = 0; i < currentOnPipleChairList.length; i++) {
            currentOnPipleChairList[i].back_timestamp = currentOnPipleChairList[i].uur_receive_return_time;
            currentOnPipleChairList[i].uur_receive_return_time = uitl.timestampToTime(currentOnPipleChairList[i].uur_receive_return_time);

            recordChairList.push(currentOnPipleChairList[i].uur_equipmnet_id);

            if (currentOnPipleChairList[i].ti_name == hospitalName) {
              ownChairCount++;
              ownBackChairList.push(currentOnPipleChairList[i]);
            } else {
              otherChairCount++;
              otherBackChairList.push(currentOnPipleChairList[i]);
            }
          }

          var commonAarr = new Array();
          for (var i = 0; i < chairList.length; i++) {
            var flg = false;
            for (var j = 0; j < recordChairList.length; j++) {
              if (recordChairList[j].toString() == chairList[i].toString()) {
                flg = true;
                break;
              }
            }
            if (flg) {
              commonAarr.push(chairList[i]);
            }
          }
          var diffArr = new Array();
          for (var i = 0; i < chairList.length; i++) {
            var flg = false;
            for (var j = 0; j < commonAarr.length; j++) {
              if (chairList[i].toString() == commonAarr[j].toString()) {
                flg = true;
                break;
              }
            }
            if (!flg) {
              diffArr.push(chairList[i]);
            }
          }


          for (var i = 0; i < recordChairList.length; i++) {
            var flg = false;
            for (var j = 0; j < commonAarr.length; j++) {
              if (recordChairList[i].toString() == commonAarr[j].toString()) {
                flg = true;
                break;
              }
            }
            if (!flg) {
              diffArr.push(recordChairList[i]);
            }
          }

          console.log("--------diffArr>", diffArr);


          that.setData({
            chairInfoList: chairInfoList,
            noBackChairList: noBackChairList,
            noBackChairOriginList: noBackChairList,
            currentOnPipleChairList: currentOnPipleChairList,
            currentOnPipleChairOriginList: currentOnPipleChairList,
            ownBackChairList: ownBackChairList,
            otherBackChairList: otherBackChairList,
            ownNoBackChairList: ownNoBackChairList,
            otherNoBackChairList: otherNoBackChairList,
            diffChairList: diffArr,
            diffChairCount: diffArr.length,
            chairCount: chairCount,
            noBackCount: noBackCount,
            backChairCount: backChairCount,
            ownChairCount: ownChairCount,
            otherChairCount: otherChairCount,
            ownNoBackChairCount: ownNoBackChairCount,
            otherNoBackChairCount: otherNoBackChairCount,
            ownBack: true,
            otherBack: true,
            ownNoBack: true,
            otherNoBack: true,
          })
        }
      })
    });  



  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 轮椅在桩上
   */
  onPiple:function(e) {

    var eqid = e.currentTarget.dataset.eqid;
    var hid = e.currentTarget.dataset.hid;
    var lbmId = this.data.lbmId;
    wx.showModal({
      title: '轮椅在桩上确认',
      content: '轮椅【' + eqid + '号】是否在桩【' + hid +'号】上?',
      success(res) {
        if (res.confirm) {
          wx.showToast({
            title: '在桩上',
          });
        } else if (res.cancel) {
        }
      }
    });

  },

  /**
   * 轮椅丢失
   */
  lose:function(e) {

    var eqid = e.currentTarget.dataset.eqid;
    var hid = e.currentTarget.dataset.hid;
    var lbmId = this.data.lbmId;
    wx.showModal({
      title: '轮椅丢失确认',
      content: '轮椅【' + eqid + '号】是否丢失?',
      success(res) {
        if (res.confirm) {
          wx.showToast({
            title: '丢失',
          });
        } else if (res.cancel) {
        }
      }
    });


  }


})